package com.jayqqaa12.common;

import java.security.interfaces.RSAPublicKey;

import org.apache.commons.codec.binary.Hex;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.google.gson.Gson;
import com.jayqqaa12.Code;
import com.jayqqaa12.jbase.jfinal.ext.ShiroExt;
import com.jayqqaa12.jbase.jfinal.ext.ctrl.JsonController;
import com.jayqqaa12.jbase.util.RSA;
import com.jayqqaa12.jbase.util.Sec;
import com.jayqqaa12.model.json.SendJson;
import com.jayqqaa12.system.model.Log;
import com.jayqqaa12.system.model.User;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.spring.Inject;

/***
 * 
 * 月落斜阳 灯火阑珊
 * 
 * @author 12
 * 
 */
@ControllerBind(controllerKey = "/")
public class IndexController extends JsonController {

	@Inject.BY_TYPE
	User userDao;
	@Inject.BY_TYPE
	Log logDao;

	public void rsa() {

		RSAPublicKey publicKey = RSA.getDefaultPublicKey();
		String modulus = new String(Hex.encodeHex(publicKey.getModulus().toByteArray()));
		String exponent = new String(Hex.encodeHex(publicKey.getPublicExponent().toByteArray()));

		SendJson json = getJsonObject();
		json.put("modulus", modulus);
		json.put("exponent", exponent);
		json.put("email", getCookie("email"));

		if (SecurityUtils.getSubject().isAuthenticated()) {
			json.setData(Consts.SESSION_USER, (User) ShiroExt.getSessionAttr(Consts.SESSION_USER));

			json.code = Code.LOGING.code;
		}

		sendJson(json);
	}


	public void jump(){
		renderError(401);
	}
	

	@Before(LoginValidator.class)
	public void login() {
		String[] result = RSA.decryptUsernameAndPwd(getPara("key"));

		try {
			UsernamePasswordToken token = new UsernamePasswordToken(result[0], Sec.md5(result[1]));
			Subject subject = SecurityUtils.getSubject();
			if (!subject.isAuthenticated()) {
				token.setRememberMe(true);
				subject.login(token);
				User u = userDao.findByEmail(result[0]);
				subject.getSession(true).setAttribute(Consts.SESSION_USER, u);
				subject.getSession(true).setAttribute(Consts.SESSION_USER_JSON, new Gson().toJson(u.getAttrs()));
			}
			if (getParaToBoolean("remember")) {
				setCookie("email", result[0], 60 * 60 * 24 * 7);
				setCookie("pwd", result[1], 60 * 60 * 24 * 7);
			} else removeCookie("email");
			logDao.insert(this, Log.EVENT_LOGIN);
			sendJson(200);
		} catch (UnknownAccountException e) {
			sendJson(Code.USER_NOT_EXIT.code);
		} catch (IncorrectCredentialsException e) {
			sendJson(Code.USER_PWD_ERROR.code);
		} catch (LockedAccountException e) {
			sendJson(Code.USER_FRRZE.code);
		} catch (ExcessiveAttemptsException e) {
			sendJson(Code.USER_ATTEMPT.code);
		} catch (AuthenticationException e) {
			sendJson(Code.USER_AUTH.code);
		} catch (Exception e) {
			sendJson(Code.SERVER_ERROR.code);
			e.printStackTrace();
		}
	}

	public void logout() {
		try {
			Subject subject = SecurityUtils.getSubject();
			subject.logout();
			sendJson();

		} catch (AuthenticationException e) {
			e.printStackTrace();
			renderText("异常：" + e.getMessage());
		}
	}

}
